 program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement = ^element;
  element = record
    a : Integer;
    b : integer;
    next : pelement;
  end;

var
  fin : textfile;
  flag : boolean;
  n, w, i, j, k, l, m, o, p: integer;
  head1, head2, head3 : pelement;
  mass1, mass2 : array [0..1000] of integer;

procedure Add (var head : pelement; num1, num2 : integer);
var tmp : pelement;
begin
  tmp := head;
  New (head);
  head^.a := num1;
  head^.b := num2;
  head^.next := tmp;
end;

procedure Multiplication (var heada, headb, headc : pelement; n, w : integer);
var tmpa, tmpb, tmpc : pelement;
    j : integer;
begin
  tmpa:=heada;
  tmpb:=headb;
  tmpc:=headc;
  for j:=0 to n-1 do
  begin
      Add(tmpc, tmpa.a*tmpb.a, tmpa.b+tmpb.b);
      tmpb:=tmpb^.next;
      mass1[w]:=tmpc.a;
      mass2[w]:=tmpc.b;
      w:=w+1;
  end;
end;

function Deletea (var head : pelement):integer;
var tmp : pelement;
begin
  if (head <> nil) then
  begin
    tmp := head^.next;
    Deletea := head^.a;
    Dispose (head);
    head := tmp;
  end;
end;

function Deleteb (var head : pelement):integer;
var tmp : pelement;
begin
  if (head <> nil) then
  begin
    tmp := head^.next;
    Deleteb := head^.b;
    Dispose (head);
    head := tmp;
  end;
end;

begin
  assignfile(fin, 'input.txt');
  reset(fin);

  readln(fin, k);
  write('0');

  for i := 0 to k-1 do
  begin
    read(fin, l);
    readln(fin, m);
    Add(head1, l, m);
  end;

  readln(fin, n);
  for i := 0 to n-1 do
  begin
    read(fin, o);
    readln(fin, p);
    Add(head2, o, p);
  end;

  for i:= 0 to k-1 do
  begin
    if(i=0) then
      w:=0;
    Multiplication (head1, head2, head3, n, w);
    head1:=head1^.next;
    w:=w+n;
  end;


  for i := 0 to (k)*(n)-1 do
  begin
    flag:=true;
    for j := 0 to (k)*(n)-1 do
    begin
      if (j>=i) then
      begin
        if (mass2[i]=mass2[j]) and (i<>j) then
        begin
          mass1[j]:=mass1[i]+mass1[j];
          flag:=false;
        end;
        if (j=(k)*(n)-1) then
          if (flag) then
          begin
            writeln('+', mass1[i], 'x^', mass2[i]);
          end;
        
      end;
    end;
  end;

  for i := 0 to k-1 do
  begin
    Deletea(head1);
    Deleteb(head1);
  end;

  for i := 0 to n-1 do
  begin
    Deletea(head2);
    Deleteb(head2);
  end;

  closefile(fin);
  readln;
end.
